<template>
	<view>
		<view class="user-section">
			<image class="bg" src="/static/user-bg.jpg"></image>
			<text class="bg-upload-btn yticon icon-paizhao"></text>
			<view class="portrait-box">
				<image class="portrait" :src="userInfo.avatarUrl || '/static/missing-face.png'"></image>
				<text class="pt-upload-btn yticon icon-paizhao"></text>
			</view>
		</view>
		<view style="margin: 20rpx;">
			<!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
			<u--form labelPosition="left" :model="model" :rules="rules" ref="form">
				<u-form-item label="姓名" prop="userInfo.nickname" borderBottom ref="item">
					<u--input v-model="model.userInfo.nickname" border="none"></u--input>
				</u-form-item>
				<!-- <u-form-item label="性别" prop="userInfo.wechat.sex" borderBottom @click="showSex = true" ref="item">
					<u--input v-model="model.userInfo.wechat.sex" disabled disabledColor="#ffffff" placeholder="请选择性别"
						border="none"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item> -->
				<u-form-item label="手机" prop="userInfo.phone" borderBottom ref="item">
					<u--input v-model="model.userInfo.phone" border="none"></u--input>
				</u-form-item>
			</u--form>
			<!-- <u-action-sheet :show="showSex" :actions="actions" title="请选择性别" description="如果选择保密会报错"
				@close="showSex = false" @select="sexSelect">
			</u-action-sheet> -->
		</view>
		<view style="display: flex;justify-content: center;">
			<view class="bottom_btn">
				<u-button type="primary" @click="submit">保存</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import common from '@/mixins/common.js'
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		getMemberProfile,
		setMemberProfile
	} from '@/service/api.js'
	export default {
		mixins: [common],
		data() {
			return {
				showSex: false,
				model: {
					userInfo: {
						name: 'my name',
						// wechat: {
						// 	sex: '0'
						// }
					},
				},
				actions: [{
						name: '男',
					},
					{
						name: '女',
					},
					{
						name: '保密',
					},
				],
				rules: {
					'userInfo.name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					// 'userInfo.wechat.sex': {
					// 	type: 'string',
					// 	max: 1,
					// 	required: true,
					// 	message: '请选择男或女',
					// 	trigger: ['blur', 'change']
					// },
					'userInfo.phone': [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					]
				},
				radio: '',
				switchVal: false
			};
		},
		onLoad() {

		},
		onShow() {
			this.fetch_data()
		},
		computed: {
			...mapState(['userInfo']),

		},
		methods: {
			// sexSelect(e) {
			// 	this.model.userInfo.wechat.sex = e.name
			// 	this.$refs.form.validateField('userInfo.wechat.sex')
			// },
			fetch_data() {
				let that = this
				getMemberProfile().then(r2 => {
					console.log(r2)
					if (r2.code == 200) {
						that.model.userInfo = r2.data
					} else {
						that.showToast(r2.msg);
					}
				})
			},
			submit() {
				let that = this
				this.$refs.form.validate().then(res => {
					console.log(res,'前端验证未生效，待修复')
					setMemberProfile(that.model.userInfo).then(r2 => {
						console.log(r2)
						if (r2.code == 200) {
							that.fetch_data()
						} else {
							that.showToast(r2.msg);
						}
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
				
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
	}

	.user-section {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 460upx;
		padding: 40upx 30upx 0;
		position: relative;

		.bg {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			filter: blur(1px);
			opacity: .7;
		}

		.portrait-box {
			width: 200upx;
			height: 200upx;
			border: 6upx solid #fff;
			border-radius: 50%;
			position: relative;
			z-index: 2;
		}

		.portrait {
			position: relative;
			width: 100%;
			height: 100%;
			border-radius: 50%;
		}

		.yticon {
			position: absolute;
			line-height: 1;
			z-index: 5;
			font-size: 48upx;
			color: #fff;
			padding: 4upx 6upx;
			border-radius: 6upx;
			background: rgba(0, 0, 0, .4);
		}

		.pt-upload-btn {
			right: 0;
			bottom: 10upx;
		}

		.bg-upload-btn {
			right: 20upx;
			bottom: 16upx;
		}
	}

	.bottom_btn {
		position: fixed;
		bottom: 0;
		width: 80%;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		box-sizing: content-box;
	}
</style>
